/** 
 * barMng for Q.Window class
 * create windows using Q.Window() [http://qutensil.com/pages/qwindow]
 * and adds buttons to a user defined bar that control windows visibility and z-index
 * http://code.google.com/p/barmng
 * demo: http://www.bradypus.net/barMng/
 * 
 * 
 * @version		0.1 [2010/10/10]
 * @author		Julian Bogdani, jbogdani@gmail.com
 * @copyright	(c) 2010 Julian Bogdani (http://www.bradypus.net)
 * @license		barMng is freely distributable under the terms of an MIT-style license.
 * @requires	Prototype
 * @requires	Scriptaculous
 * @requires	Q.Window
 * @since		2010/10/10
 * 
 * changelog
 * 0.1, [2010/10/10]	
 * */

var barMng = Class.create({
	/**
	 * @param string barId	id of the bar to populate
	 */
	initialize: function(barId){
		this.bar = $(barId);
	},
	/**
	 * starts Q.Window and adds controler button to bar
	 * 
	 * @param object opt	options for Q.Window()
	 */
	win: function(opt){
		//default options for win
		opt = $H({
			text:'Hello world - OBJ MNG',
			title:'win Title',
			height:500
		}).merge(opt).toObject();

		//create win with options
		var win = new Q.Window(opt);

		//add to bar function
		this.addObj2bar(win, opt.title);
	},
	/**
	 * adds button to bar for window control
	 *
	 * @param object win	window object
	 * @param string title	title of the window
	 * @todo				get title dinamically from Q.Window class (add method getTitle()?)
	 */
	addObj2bar: function(win, title){
		this.bar.insert({
			bottom: new Element('button')
				.writeAttribute('type', 'button')
				.update(title)
				.observe('click', function(){
					if(win.visible()){
						if(win.holder.style.zIndex == 99996 || win.holder.style.zIndex == 99998){
							// should work without the following row, but it doesn't!
							win.show();
							win.hide();
						} else {
							win.restack();
						}
					} else {
						win.show();
						win.restack();
					} 
				}.bind(win))
		});
	},
	/**
	 * temporary, debugging function
	 * Takes object and returns string
	 * If flag alert is true, alerts the string
	 * 
	 * @param object obj		object to debug
	 * @param string alert		control flag
	 */
	debug: function(obj, alert){
		var tmp = '';
		for(var a in obj){
			tmp+=a+': '+obj.a+'\n';
		}
		if(alert) alert(tmp);
		return tmp;
	}
});